package com.dofast.bt.mvp.view.activity.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.Loader;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.mvp.presenter.main.MainPresenter;
import com.dofast.bt.mvp.view.fragment.mian.BasketFragment;
import com.dofast.bt.mvp.view.fragment.mian.MessageFragment;
import com.dofast.bt.mvp.view.fragment.mian.MyFragment;
import com.dofast.bt.mvp.view.fragment.mian.ShopFragment;
import com.dofast.bt.util.Helper;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/17 0017.
 * class :主界面
 */

public class MainActivity extends BaseMvpActivity<MainPresenter, MainView> implements MainView {


    @BindView(R.id.imv_bottom_bar_icon_shop)
    ImageView imvBottomBarIconShop;
    @BindView(R.id.txv_bottom_bar_name_one)
    TextView txvBottomBarNameOne;
    @BindView(R.id.btn_shop)
    LinearLayout btnShop;
    @BindView(R.id.imv_bottom_bar_icon_message)
    ImageView imvBottomBarIconMessage;
    @BindView(R.id.txv_bottom_bar_name_two)
    TextView txvBottomBarNameTwo;
    @BindView(R.id.btn_pager_message)
    LinearLayout btnPagerMessage;
    @BindView(R.id.imv_bottom_bar_icon_basket)
    ImageView imvBottomBarIconBasket;
    @BindView(R.id.txv_bottom_bar_name_three)
    TextView txvBottomBarNameThree;
    @BindView(R.id.btn_pager_basket)
    LinearLayout btnPagerBasket;
    @BindView(R.id.imv_bottom_bar_icon_my)
    ImageView imvBottomBarIconMy;
    @BindView(R.id.txv_bottom_bar_name_four)
    TextView txvBottomBarNameFour;
    @BindView(R.id.btn_pager_my)
    LinearLayout btnPagerMy;
    @BindView(R.id.bottom_guide_bar)
    LinearLayout bottomGuideBar;
    @BindView(R.id.fragment_act_home)
    RelativeLayout fragmentActHome;
    /**
     * 茶铺
     */
    private Fragment fragmentShop;
    /**
     * 资讯
     */
    private Fragment fragmentMessage;
    /**
     * 茶篓
     */
    private Fragment fragmentBasket;
    /**
     * 我
     */
    private Fragment fragmentMy;

    @Override
    protected int getResourcesId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        presenter.init();
    }

    @Override
    protected void addOnClickListener() {

    }

    @Override
    public Loader<MainPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<MainPresenter>() {
            @Override
            public MainPresenter create() {
                return new MainPresenter();
            }
        });
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.btn_shop, R.id.btn_pager_message, R.id.btn_pager_basket, R.id.btn_pager_my})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_shop:
                presenter.showShopFragment();
                break;
            case R.id.btn_pager_message:
                presenter.showMessageFragment();
                break;
            case R.id.btn_pager_basket:
                presenter.showBasketragment();
                break;
            case R.id.btn_pager_my:
                presenter.showMyFragment();
                break;
        }
    }

    @Override
    public void showShopFragment() {
        if (null == fragmentShop) {
            fragmentShop = new ShopFragment();
        }
        Helper.changeFragment(this, R.id.fragment_act_home, fragmentShop, 0);
    }


    @Override
    public void showMessageFragment() {
        if (null == fragmentMessage) {
            fragmentMessage = new MessageFragment();
        }
        Helper.changeFragment(this, R.id.fragment_act_home, fragmentMessage, 0);
    }

    @Override
    public void showBasketragment() {
        if (null == fragmentBasket) {
            fragmentBasket = new BasketFragment();
        }
        Helper.changeFragment(this, R.id.fragment_act_home, fragmentBasket, 0);
    }

    @Override
    public void showMyFragment() {
        if (null == fragmentMy) {
            fragmentMy = new MyFragment();
        }
        Helper.changeFragment(this, R.id.fragment_act_home, fragmentMy, 0);
    }

    @Override
    public void initBottom() {
        imvBottomBarIconShop.setImageResource(R.mipmap.shop_default);
        imvBottomBarIconMessage.setImageResource(R.mipmap.message_default);
        imvBottomBarIconBasket.setImageResource(R.mipmap.basket_default);
        imvBottomBarIconMy.setImageResource(R.mipmap.my_default);
    }

    @Override
    public void selectShop() {
        initBottom();
        imvBottomBarIconShop.setImageResource(R.mipmap.shop_nomal);
    }

    @Override
    public void selectMessage() {
        initBottom();
        imvBottomBarIconMessage.setImageResource(R.mipmap.message_normal);

    }

    @Override
    public void selectBasket() {
        initBottom();
        imvBottomBarIconBasket.setImageResource(R.mipmap.basket_normal);

    }

    @Override
    public void selectMy() {
        initBottom();
        imvBottomBarIconMy.setImageResource(R.mipmap.my_normal);
    }

    private long temp;

    /**
     * 双击退出
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == event.getKeyCode() ) {
            if (System.currentTimeMillis() - temp < 2000) {
                finish();
            } else {
                temp = System.currentTimeMillis();
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_LONG).show();
            }
        }
        return true;
    }
}
